<?php
$page_type = "";
require_once 'header.php';

echo "<h3>Enrollment Main</h3>";

//Generate the dropdown menu for Semester
$sql = "select semester from COURSE_OFFERING group by semester";

$result_array = execute_sql_in_oracle($sql);
$result = $result_array["flag"];
$cursor = $result_array["cursor"];

if ($result == false) {
  display_oracle_error_message($cursor);
  die("Query failed to find Semester.");
}

echo("
  <form method=\"post\" action=\"enroll.php\">
  Semester:  
  <select name=\"input_semester\">
  <option value=\"\">All</option>
");

//Fetch available Semesters and load into dropdown menu
while($values = oci_fetch_array($cursor)) {
  $semester = $values[0];
  echo("
    <option value=\"$semester\">$semester</option>
  ");
}
oci_free_statement($cursor);

echo("
  </select>
  Course Number: <input type=\"text\" size=\"10\" maxlength=\"10\" name=\"input_cno\">
  <input type=\"submit\" value=\"Search\">
  </form>
");

//Interpret the query
$input_semester = "";
$input_cno = "";

if (isset($_POST["input_semester"]))
  $input_semester = $_POST["input_semester"];
if (isset($_POST["input_cno"]))
  $input_cno = $_POST["input_cno"];

$whereclause = "1=1 ";

if (isset($input_semester) and trim($input_semester) != "")
  $whereclause .= " and semester='$input_semester'";
if (isset($input_cno) and trim($input_cno) != "")
  $whereclause .= " and cd.cno like '%$input_cno%'";

//Form the query
$sql = "select seqid, cd.cno, Title, Credit, Semester, Time, max_seats - (select count(*) from TAKEN t where t.seqid = co.seqid) as avail_seats, max_seats, deadline from COURSE_DESCRIPTION cd, COURSE_OFFERING co where cd.cno = co.cno and $whereclause";

$result_array = execute_sql_in_oracle($sql);
$result = $result_array["flag"];
$cursor = $result_array["cursor"];

if (!$result) {
  display_oracle_error_message($cursor);
  die("Client query failed.");
}

//Display the results
echo(" <br><form method=\"post\" action=\"enroll_add.php\">
<table border=1>
<tr><th></th><th>Course ID</th><th>Course Number</th><th>Title</th> <th>Credit</th><th>Semester</th><th>Time</th><th>Available Seats</th><th>Maximum Seats</th><th>Deadline</th></tr>
");
while($values = oci_fetch_array($cursor)) {
  $t_cid = $values[0];
  $t_cno = $values[1];
  $t_title = $values[2];
  $t_credit = $values[3];
  $t_semester = $values[4];
  $t_time = $values[5];
  $t_available = $values[6];
  $t_max = $values[7];
  $t_deadline = $values[8];

  echo("<tr><td> <input type=\"checkbox\" name=\"input_enroll[]\" value=\"$t_cid\"></td>
<td>$t_cid</td> <td>$t_cno</td> <td>$t_title</td> <td>$t_credit</td> <td>$t_semester</td> <td>$t_time</td> <td>$t_available</td> <td>$t_max</td><td>$t_deadline</td> </tr>");
}
echo("</table> <br><input type=\"submit\" value=\"Enroll\"></form>");

oci_free_statement($cursor);




?>
</body>
</html>
